﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ModemGSM;

namespace PhoneManager
{
    public partial class FrmReport : PhoneManager.FrmDefault
    {
        int _TimerCount = 0;
        bool _Edit = false;
        bool _Load = false;

        public FrmReport()
        {
            InitializeComponent();
        }

        private void BntCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmReport_Load(object sender, EventArgs e)
        {
            CboFilter.SelectedIndex = 0;
            PrgTime.Minimum = 0;
            PrgTime.Maximum = (int)UpDownTimer.Value;
            PrgTime.Value = 0;
            timerReport.Stop();
        }

        private void FrmReport_Shown(object sender, EventArgs e)
        {
            LoadReport();
            timerReport.Interval = 50;
            timerReport.Start();
            _Load = true;
        }

        private void BntRefresh_Click(object sender, EventArgs e)
        {
            bool TmOK = timerReport.Enabled;
            timerReport.Stop();
            LoadReport();
            if (TmOK)
            {
                timerReport.Interval = 50;
                _TimerCount = 0;
                timerReport.Start();
            }
        }

        private void timerReport_Tick(object sender, EventArgs e)
        {
            if (timerReport.Interval != 1000)
            {
                timerReport.Interval = 1000;
            }
            _TimerCount++;
            if (_TimerCount <= PrgTime.Maximum)
            {
                PrgTime.Value = _TimerCount;
            }
            else
            {
                PrgTime.Value = PrgTime.Maximum;
            }
            if (_TimerCount >= (int)UpDownTimer.Value)
            {
                _TimerCount = 0;
                timerReport.Stop();
                LoadReport();
                timerReport.Start();
            }
        }

        private void PctStop_Click(object sender, EventArgs e)
        {
            PctStop.Image = Properties.Resources.pauseOff;;
            PctStart.Image = Properties.Resources.play;
            timerReport.Stop();
            foreach (UCReport item in flowLayoutPanelReport.Controls)
            {
                item.ReadOnly = false;
            }
            _Edit = true;
            _TimerCount = 0;
            PrgTime.Value = _TimerCount;
            PctStop.Enabled = false;
            PctStart.Enabled = true;
            lnkAllBox.Enabled = true;
            lnkUnAllBox.Enabled = true;
        }

        private void PctStart_Click(object sender, EventArgs e)
        {
            PctStop.Image = Properties.Resources.pause; 
            PctStart.Image = Properties.Resources.playOff;
            timerReport.Interval = 50;
            _TimerCount = (int)UpDownTimer.Value;
            foreach (UCReport item in flowLayoutPanelReport.Controls)
            {
                item.Seleted = false;
                item.ReadOnly = true;
            }
            _Edit = false;
            lnkAllBox.Enabled = false;
            lnkUnAllBox.Enabled = false;
            BntDelete.Enabled = false;
            timerReport.Start();
            PctStop.Enabled = true;
            PctStart.Enabled = false;
        }

        private void UpDownTimer_ValueChanged(object sender, EventArgs e)
        {
            PrgTime.Minimum = 0;
            PrgTime.Maximum = (int)UpDownTimer.Value;
        }

        private void CboFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_Load)
            {
                bool TmOK = timerReport.Enabled;
                timerReport.Stop();
                this.Cursor = Cursors.WaitCursor;
                LoadReport();
                if (TmOK)
                {
                    timerReport.Interval = 50;
                    _TimerCount = 0;
                    timerReport.Start();
                }
                this.Cursor = Cursors.Default;
            }
        }
        private void LoadReport()
        {
            SharedLib.QtdNewSMSReport = 0;
            SharedLib.MainForm.timerStatus.Stop();
            SharedLib.StartWaitProcessThread();
            SharedLib.EndWaitProcessThread();
            BntDelete.Enabled = false;
            BntRefresh.Enabled = false;
            this.Cursor = Cursors.WaitCursor;
            GroupReportEnum Filter1 = GroupReportEnum.None;
            GroupReportEnum Filter2 = GroupReportEnum.None;
            if (CboFilter.SelectedIndex == 1)
            {
                Filter1 = GroupReportEnum.WaitReport;
                Filter2 = GroupReportEnum.WaitReport;
            }
            else if (CboFilter.SelectedIndex == 2)
            {
                Filter1 = GroupReportEnum.ReceiverOk;
                Filter2 = GroupReportEnum.ReceiverOk;
            }
            else if (CboFilter.SelectedIndex == 3)
            {
                Filter1 = GroupReportEnum.TemporaryErrorAllowTry;
                Filter2 = GroupReportEnum.TemporaryErrorAllowTry;
            }
            else if (CboFilter.SelectedIndex == 4)
            {
                Filter1 = GroupReportEnum.PermanentErrorNotTry;
                Filter2 = GroupReportEnum.TemporaryErrorNotTry;
            }
            flowLayoutPanelReport.Controls.Clear();
            List<StatusSMSReport> items = SharedLib.ModemGSM.SMSStatusReport;
            items.Sort(new OrderReportSMSDesc());
            foreach (StatusSMSReport item in items)
            {
                bool ok = false;
                if (Filter1 == GroupReportEnum.None || Filter2 == GroupReportEnum.None)
                {
                    ok = true;
                }
                else if (item.GroupReport == Filter1 || item.GroupReport == Filter2)
                {
                    ok = true;
                }
                if (ok)
                {
                    UCReport uc = new UCReport();
                    uc.Checked = false;
                    uc.TypeReport = TypeStatusReportEnum.SendConfirmation;
                    uc.TypeCode = item.TypeCode;
                    uc.Storage = item.Storage;
                    uc.IndexStorage = item.IndexStorage;
                    uc.DateTimeSent = item.DateTimeSend;
                    uc.DateTimeRec = item.DateTimeReceiver;
                    uc.Number = item.Number;
                    uc.Message = item.MessageSend;
                    uc.TimezoneSent = item.TimezoneSend;
                    uc.TimezoneRec = item.TimezoneReceiver;
                    uc.Seleted = false;
                    uc.Status = item.Status;
                    uc.ReadOnly = !_Edit;
                    uc.StatusMessage = item.MessageStatus;
                    uc.EventUCReportChecked += new UCReport.UCReportCheckedEventHandler(uc_EventUCReportChecked);
                    flowLayoutPanelReport.Controls.Add(uc);
                }
            }

            this.Cursor = Cursors.Default;
            BntRefresh.Enabled = true;
            SharedLib.MainForm.ShowReportCount();
            SharedLib.MainForm.timerStatus.Start();
        }

        private void uc_EventUCReportChecked(object sender, EventArgs e)
        {
            UCReport UC = (UCReport)sender;
            if (UC.Checked)
            {
                BntDelete.Enabled = true;
            }
            else
            {
                bool temchecked = false;
                foreach (UCReport item in flowLayoutPanelReport.Controls)
                {
                    if (item.Checked)
                    {
                        temchecked = true;
                        break;
                    }
                }
                if (temchecked)
                {
                    BntDelete.Enabled = true;
                }
                else
                {
                    BntDelete.Enabled = false;
                }
            }
        }

        private void lnkAllBox_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            foreach (UCReport item in flowLayoutPanelReport.Controls)
            {
                item.Checked = true;
            }
        }

        private void lnkUnAllBox_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            foreach (UCReport item in flowLayoutPanelReport.Controls)
            {
                item.Checked = false;
            }
        }

        private void BntDelete_Click(object sender, EventArgs e)
        {
            SharedLib.MainForm.timerStatus.Stop();
            SharedLib.StartWaitProcessThread();
            SharedLib.EndWaitProcessThread();

            bool TmOK = timerReport.Enabled;
            timerReport.Stop();
            bool temchecked = false;
            foreach (UCReport item in flowLayoutPanelReport.Controls)
            {
                if (item.Checked)
                {
                    StatusSMSReport result = SharedLib.ModemGSM.SMSFindStatusReportByNumberDateSend(item.Number,item.DateTimeSent.Value);
                    if (result != null)
                    {
                        temchecked = true;
                        SharedLib.ModemGSM.SMSRemoveStatusReport(result);
                    }
                }
            }
            if (temchecked)
            {
                LoadReport();
                _TimerCount = 0;
            }
            if (TmOK)
            {
                timerReport.Interval = 50;
                timerReport.Start();
            }
            SharedLib.MainForm.timerStatus.Start();
        }

        private void FrmReport_FormClosing(object sender, FormClosingEventArgs e)
        {
            timerReport.Stop();
        }

    }
}
